﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class usermanager : Form
    {
        public usermanager()
        {
            InitializeComponent();
        }

        private void usermanager_Load(object sender, EventArgs e)
        {
            // 把行数给显示出来
            Read_DB_Fill_Grid("usertable",dataGridView1);
        }

        private void Set_Color(DataGridView datagridview1)
        {
             for(int i=0;i<datagridview1.RowCount;i++)
             {
                 for(int j=0;j<datagridview1.ColumnCount;j++)
                 {
                     if (i % 2 == 0) datagridview1[j, i].Style.BackColor = Color.LightBlue;
                 }
             }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Read_DB_Fill_Grid(string table_name,DataGridView datagridview1)
        {
            // 用数据库将表格填满
            try
            {
                DataSet ds = MainView.mysql.sql_search_database("Select * from usertable");
                int colnum = ds.Tables[0].Columns.Count;         // 列数
                datagridview1.ColumnCount = colnum;              // 设置列数
                int rownum = ds.Tables[0].Rows.Count;            // 行数
                datagridview1.RowCount = rownum;                 // 设置行数
                for(int i=0;i<rownum;i++)
                {
                    for(int j=0;j<colnum;j++)
                    {
                        datagridview1[j, i].Value = ds.Tables[0].Rows[i][j].ToString();     // 把那一行显示出来
                    }
                }
            }
            catch {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        datagridview1[j, i].Value = "";     // 把那一行显示出来
                    }
                }  

            }
            Set_Color(datagridview1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 弹出增加用户界面
            Add_user myview = new Add_user();
            myview.ShowDialog();
            Read_DB_Fill_Grid("usertable", dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                string namestring = dataGridView1[0, selected_index].Value.ToString();  // 用户名
                string passstring = dataGridView1[1, selected_index].Value.ToString();  // 密码
                string limit = dataGridView1[2, selected_index].Value.ToString();       // 权限
                UserEdit usereditview = new UserEdit();
                usereditview.originname = namestring;
                usereditview.originpassword = passstring;
                usereditview.originlimit = limit;
                usereditview.Fill_Info();
                usereditview.ShowDialog();
                Read_DB_Fill_Grid("usertable", dataGridView1);
            }
            catch { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                string namestring = dataGridView1[0, selected_index].Value.ToString();   // 用户名
                DeleteUserConfig myview = new DeleteUserConfig();
                myview.originname = namestring;
                myview.ShowDialog();
                Read_DB_Fill_Grid("usertable", dataGridView1);
            }
            catch{}
        }
    }
}
